import 'package:flutter_app/common/global.dart';
import 'dart:convert';

import 'package:flutter_app/tools/utils.dart';

class Tweet {
  String id;
  String userName;
  String text;
  List<String> images;
  List<String> thumbnails;
  // final bool isVoted = false;
  bool isFavorited;
  int favoriteCount;
  bool dissatisfied;
  int dissatisfiedCount;
  bool isMarked;
  int markCount;
  int repliesCount;
  DateTime inTime = DateTime.now();
  String timeStr;

  Tweet(
      {this.id,
      this.userName = "匿名者",
      this.text = "",
      this.images,
      this.thumbnails,
      this.isFavorited = false,
      this.favoriteCount = 0,
      this.dissatisfied = false,
      this.dissatisfiedCount = 0,
      this.isMarked = false,
      this.markCount = 0,
      this.repliesCount = 0,
      this.inTime,
      this.timeStr = ""}) {
    if (images == null) {
      this.images = [];
      this.thumbnails = [];
    } else {
      for (var i = 0; i < images.length; i++) {
        images[i] = Global.imageUrlPath + images[i];
        thumbnails[i] = Global.imageUrlPath + thumbnails[i];
      }
    }

    if (this.inTime == null) this.inTime = DateTime.now();
  }

  factory Tweet.fromJson(dynamic jsonObj) {
    return new Tweet(
        id: jsonObj["Id"],
        text: jsonObj["Text"],
        userName: jsonObj["Issuer"]["NickName"] == ""
            ? "匿名者"
            : jsonObj["Issuer"]["NickName"],
        images: json.decode(jsonObj["Images"]).cast<String>(),
        thumbnails: json.decode(jsonObj["Thumbnails"]).cast<String>(),
        isFavorited: jsonObj["IsFavorited"],
        dissatisfied: jsonObj["Dissatisfied"],
        favoriteCount: jsonObj["LikedCount"],
        dissatisfiedCount: jsonObj["DissatisfiedCount"],
        repliesCount: jsonObj["RepliesCount"],
        inTime: DateTime.parse(jsonObj["InTime"]));
  }
}

class Reply {
  String id;
  String tweetId;
  String userName;
  String text;

  bool isFavorited;
  int favoriteCount;
  bool dissatisfied;
  int dissatisfiedCount;
  DateTime inTime = DateTime.now();
  String timeStr;

  Reply(
      {this.id,
      this.tweetId,
      this.userName = "匿名者",
      this.text = "",
      this.isFavorited = false,
      this.favoriteCount = 0,
      this.dissatisfied = false,
      this.dissatisfiedCount = 0,
      this.inTime,
      this.timeStr = ""}) {
    if (this.inTime == null) this.inTime = DateTime.now();
  }

  factory Reply.fromJson(dynamic jsonObj) {
    return new Reply(
        id: jsonObj["Id"],
        text: jsonObj["Text"],
        userName: jsonObj["Issuer"]["NickName"] == ""
            ? "匿名者"
            : jsonObj["Issuer"]["NickName"],
        isFavorited: jsonObj["IsFavorited"],
        dissatisfied: jsonObj["Dissatisfied"],
        favoriteCount: jsonObj["LikedCount"],
        dissatisfiedCount: jsonObj["DissatisfiedCount"],
        inTime: DateTime.parse(jsonObj["InTime"]));
  }
}
